.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Portions Copyright (c) 1999, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH SOCKET 3XNET "Jun 10, 2002"
.SH NAME
socket \- create an endpoint for communication
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lxnet\fR [ \fIlibrary\fR ... ]
#include <sys/socket.h>

\fBint\fR \fBsocket\fR(\fBint\fR \fIdomain\fR, \fBint\fR \fItype\fR, \fBint\fR \fIprotocol\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBsocket()\fR function creates an unbound socket in a communications
domain, and returns a file descriptor that can be used in later function calls
that operate on sockets.
.sp
.LP
The <\fBsys/socket.h\fR> header defines at least the following values for the
\fIdomain\fR argument:
.sp
.ne 2
.na
\fB\fBAF_UNIX\fR\fR
.ad
.RS 12n
File system pathnames.
.RE

.sp
.ne 2
.na
\fB\fBAF_INET\fR\fR
.ad
.RS 12n
Internet Protocol version 4 (IPv4) address.
.RE

.sp
.ne 2
.na
\fB\fBAF_INET6\fR\fR
.ad
.RS 12n
Internet Protocol version 6 (IPv6) address.
.RE

.sp
.LP
The \fItype\fR argument specifies the socket type, which determines the
semantics of communication over the socket.  The socket types supported by the
system are implementation-dependent.  Possible socket types include:
.sp
.ne 2
.na
\fB\fBSOCK_STREAM\fR\fR
.ad
.RS 18n
Provides sequenced, reliable, bidirectional, connection-mode byte streams, and
may provide a transmission mechanism for out-of-band data.
.RE

.sp
.ne 2
.na
\fB\fBSOCK_DGRAM\fR\fR
.ad
.RS 18n
Provides datagrams, which are connectionless-mode, unreliable messages of fixed
maximum length.
.RE

.sp
.ne 2
.na
\fB\fBSOCK_SEQPACKET\fR\fR
.ad
.RS 18n
Provides sequenced, reliable, bidirectional, connection-mode transmission path
for records.  A record can be sent using one or more output operations and
received using one or more input operations, but a single operation never
transfers part of more than one record.  Record boundaries are visible to the
receiver via the MSG_EOR flag.
.RE

.sp
.LP
If the \fIprotocol\fR argument is non-zero, it must specify a protocol that is
supported by the address family.  The protocols supported by the system are
implementation-dependent.
.sp
.LP
The process may need to have appropriate privileges to use the \fBsocket()\fR
function or to create some sockets.
.SH PARAMETERS
.sp
.LP
The function takes the following arguments:
.sp
.ne 2
.na
\fB\fIdomain\fR\fR
.ad
.RS 12n
Specifies the communications domain in which a socket is to be created.
.RE

.sp
.ne 2
.na
\fB\fItype\fR\fR
.ad
.RS 12n
Specifies the type of socket to be created.
.RE

.sp
.ne 2
.na
\fB\fIprotocol\fR\fR
.ad
.RS 12n
Specifies a particular protocol to be used with the socket. Specifying a
\fIprotocol\fR of 0 causes \fBsocket()\fR to use an unspecified default
protocol appropriate for the requested socket type.
.RE

.sp
.LP
The \fIdomain\fR argument specifies the address family used in the
communications domain.  The address families supported by the system are
implementation-dependent.
.SH USAGE
.sp
.LP
The documentation for specific address families specify which protocols each
address family supports.  The documentation for specific protocols specify
which socket types each protocol supports.
.sp
.LP
The application can determine if an address family is supported by trying to
create a socket with \fIdomain\fR set to the protocol in question.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fBsocket()\fR returns a nonnegative integer, the
socket file descriptor.  Otherwise a value of \fB-1\fR is returned and
\fBerrno\fR is set to indicate the error.
.SH ERRORS
.sp
.LP
The \fBsocket()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEAFNOSUPPORT\fR\fR
.ad
.RS 19n
The implementation does not support the specified address family.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 19n
No more file descriptors are available for this process.
.RE

.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 19n
No more file descriptors are available for the system.
.RE

.sp
.ne 2
.na
\fB\fBEPROTONOSUPPORT\fR\fR
.ad
.RS 19n
The protocol is not supported by the address family, or the protocol is not
supported by the implementation.
.RE

.sp
.ne 2
.na
\fB\fBEPROTOTYPE\fR\fR
.ad
.RS 19n
The socket type is not supported by the protocol.
.RE

.sp
.LP
The \fBsocket()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 11n
The process does not have appropriate privileges.
.RE

.sp
.ne 2
.na
\fB\fBENOBUFS\fR\fR
.ad
.RS 11n
Insufficient resources were available in the system to perform the operation.
.RE

.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 11n
Insufficient memory was available to fulfill the request.
.RE

.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 11n
There were insufficient STREAMS resources available for the operation to
complete.
.RE

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
.BR accept (3XNET),
.BR bind (3XNET),
.BR connect (3XNET),
.BR getsockname (3XNET),
.BR getsockopt (3XNET),
.BR listen (3XNET),
.BR recv (3XNET),
.BR recvfrom (3XNET),
.BR recvmsg (3XNET),
.BR send (3XNET),
.BR sendmsg (3XNET),
.BR setsockopt (3XNET),
.BR shutdown (3XNET),
.BR socketpair (3XNET),
.BR attributes (7),
.BR standards (7)
